/**
 * 
 */
$(document).ready(function () {
	
	$('#debug').click(function () {
		if ($('#article_title').val() == '') {
			alert('The title can not be empty!');
			return false;
		}
		$('#content').html('');
		var article = $("<article></article>");
		article.append('<h1 class="title">' + $('#article_title').val() + '</h1><hr>');
		$(article).append($('#article_content').val());
		var article_wrapper = $('<div class="article-wrapper">');
		article_wrapper.append(article);
		$('.editor').fadeOut(500, function () {
			$('.maximize').show();
			$('#content').append(article_wrapper);
		});
	});
	
	$('#clear').click(function () {
		$('#content').html('');
	});
	
	$('textarea').keydown(function (e) {
		if (event.keyCode == 9) {
			var originalText = $(this).val();
			var text = '    ';
			var selectionStart = this.selectionStart;
			var selectionEnd = this.selectionEnd;
			$(this).val(originalText.substring(0, selectionStart) + text + originalText.substring(selectionStart));
			this.selectionStart = selectionStart + 4;
			this.selectionEnd = selectionStart + 4;
			return false;
		}
	});
	
	$('#minimize').click(function () {
		$('.editor').fadeOut(500, function () {
			$('#maximize').show();
		});
	});
	
	$('#maximize').click(function () {
		$('#maximize').hide();
		$('.editor').fadeIn(500);
	});
	
	$(document).click(function (e) {
		var editor = $(e.target).closest('.editor');
		if (e.target != $('#maximize')[0] && editor.length == 0) {
			$('.editor').fadeOut(500, function () {
				$('#maximize').show();
			});
		}
	});
	
	$('#submit').click(function () {
		if ($('#article_title').val() == '') {
			alert('The title can not be empty!');
			return;
		}
		if ($('#article_categories').val() == '') {
			alert('The categories can not be empty!');
			return;
		}
		if ($('#content .article-wrapper').length == 0) {
			alert('Please preview, then submit!');
			return;
		}
		$.ajax({
			url: absoluteURL('articles/save'),
			data: {
				'title': $('#article_title').val(),
				'categories': $('#article_categories').val(),
				'content': $('#content').html()
			},
			dataType: 'json',
			success: function (response) {
				if (response.status == 0) {
					localStorage.removeItem('article_title');
					localStorage.removeItem('article_categories');
					localStorage.removeItem('article_content');
					alert('Article created successfully.');
					location = absoluteURL('/menu');
				} else {
					alert(response.error.message);
				}
			}
		});
	});
	
	$('#article_title, #article_categories, #article_content').change(function () {
		localStorage.setItem(this.id, $(this).val());
	});
	
	if (localStorage.getItem('article_content') != null) {
		$('#article_content').val(localStorage.getItem('article_content'));
	}
	
	if (localStorage.getItem('article_categories') != null) {
		$('#article_categories').val(localStorage.getItem('article_categories'));
	}
	
	if (localStorage.getItem('article_title') != null) {
		$('#article_title').val(localStorage.getItem('article_title'));
	}
});